package com.city.java.orders.controller;

import com.city.java.orders.pojo.entity.Orders;
import com.city.java.orders.service.OrderService;
import com.city.java.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 订单控制器
 * RestController注解: 组合注解，相当于@Controller + @ResponseBody
 * Api注解: Knife4j文档中模块的显示名称
 * RequestMapping注解: 设置统一的请求路径前缀
 */
@Slf4j
@Api(tags = "04.订单管理模块")
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * PostMapping注解: 处理POST请求
     * ApiOperation注解: Knife4j文档中接口的显示名称
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加订单", notes = "添加新订单信息")
    public JsonResult addOrder(
            @ApiParam(value = "订单信息", required = true)
            @RequestBody Orders order) {
        log.info("接收到添加订单请求: {}", order);
        return orderService.addOrder(order);
    }

    /**
     * 根据订单ID查询订单
     * GetMapping注解: 处理GET请求
     * PathVariable注解: 获取路径中的参数
     */
    @GetMapping("/select/id/{orderId}")
    @ApiOperation(value = "根据ID查询订单", notes = "根据订单ID查询订单详细信息")
    public JsonResult getOrderById(
            @ApiParam(value = "订单ID", required = true, example = "1")
            @PathVariable Long orderId) {
        log.info("接收到根据ID查询订单请求: ID={}", orderId);
        return orderService.getOrderById(orderId);
    }

    /**
     * 查询所有订单列表
     */
    @GetMapping("/select/all")
    @ApiOperation(value = "查询所有订单", notes = "查询所有订单列表")
    public JsonResult getAllOrders() {
        log.info("接收到查询所有订单请求");
        return orderService.getAllOrders();
    }

    /**
     * 根据用户ID查询订单列表
     */
    @GetMapping("/select/user/{userId}")
    @ApiOperation(value = "根据用户ID查询订单", notes = "根据用户ID查询订单列表")
    public JsonResult getOrdersByUserId(
            @ApiParam(value = "用户ID", required = true, example = "1")
            @PathVariable Long userId) {
        log.info("接收到根据用户ID查询订单请求: userId={}", userId);
        return orderService.getOrdersByUserId(userId);
    }

    /**
     * 根据商品ID查询订单列表
     */
    @GetMapping("/select/product/{productId}")
    @ApiOperation(value = "根据商品ID查询订单", notes = "根据商品ID查询订单列表")
    public JsonResult getOrdersByProductId(
            @ApiParam(value = "商品ID", required = true, example = "1")
            @PathVariable Long productId) {
        log.info("接收到根据商品ID查询订单请求: productId={}", productId);
        return orderService.getOrdersByProductId(productId);
    }

    /**
     * 根据商品名称查询订单列表
     */
    @GetMapping("/select/product-name")
    @ApiOperation(value = "根据商品名称查询订单", notes = "根据商品名称查询订单列表")
    public JsonResult getOrdersByProductName(
            @ApiParam(value = "商品名称", required = true, example = "洗衣液")
            @RequestParam String productName) {
        log.info("接收到根据商品名称查询订单请求: productName={}", productName);
        return orderService.getOrdersByProductName(productName);
    }

    /**
     * 根据订单创建时间范围查询订单列表
     */
    @GetMapping("/select/date-range")
    @ApiOperation(value = "根据订单创建时间范围查询订单", notes = "根据订单创建时间范围查询订单列表")
    public JsonResult getOrdersByDateRange(
            @ApiParam(value = "开始时间", example = "2023-01-01 00:00:00")
            @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间", example = "2023-12-31 23:59:59")
            @RequestParam(required = false) String endTime) {
        log.info("接收到根据订单创建时间范围查询订单请求: startTime={}, endTime={}", startTime, endTime);

        LocalDateTime start = null;
        LocalDateTime end = null;

        if (startTime != null && !startTime.isEmpty()) {
            start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        if (endTime != null && !endTime.isEmpty()) {
            end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        return orderService.getOrdersByDateRange(start, end);
    }

    /**
     * 根据商品名称和时间范围查询订单列表
     */
        @GetMapping("/select/conditions")
    @ApiOperation(value = "根据商品名称和时间范围查询订单", notes = "根据商品名称和时间范围查询订单列表")
    public JsonResult getOrdersByConditions(
            @ApiParam(value = "商品名称")
            @RequestParam(required = false) String productName,
            @ApiParam(value = "开始时间", example = "2023-01-01 00:00:00")
            @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间", example = "2023-12-31 23:59:59")
            @RequestParam(required = false) String endTime,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        log.info("接收到根据商品名称和时间范围查询订单请求: productName={}, startTime={}, endTime={}",
                productName, startTime, endTime);

        LocalDateTime start = null;
        LocalDateTime end = null;

        if (startTime != null && !startTime.isEmpty()) {
            start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        if (endTime != null && !endTime.isEmpty()) {
            end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        return JsonResult.ok(orderService.getOrdersByConditions(productName, start, end, pageNum, pageSize));
    }

    /**
     * 根据订单ID删除订单
     * DeleteMapping注解: 处理DELETE请求
     */
    @DeleteMapping("/delete/id/{orderId}")
    @ApiOperation(value = "根据ID删除订单", notes = "根据订单ID删除订单")
    public JsonResult deleteOrderById(
            @ApiParam(value = "订单ID", required = true, example = "1")
            @PathVariable Long orderId) {
        log.info("接收到根据ID删除订单请求: ID={}", orderId);
        return orderService.deleteOrderById(orderId);
    }

    /**
     * 更新订单信息
     * PutMapping注解: 处理PUT请求
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新订单信息", notes = "更新订单详细信息")
    public JsonResult updateOrder(
            @ApiParam(value = "订单信息", required = true)
            @RequestBody Orders order) {
        log.info("接收到更新订单信息请求: {}", order);
        return orderService.updateOrder(order);
    }
}
